home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4657 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: nioz.nl!news
  2. From: rikko@nioz.nl (Rikko Verrijzer)
  3. Newsgroups: comp.lang.c
  4. Subject: freeing structures
  5. Date: 6 Feb 1996 10:03:15 GMT
  6. Organization: Netherlands Institute for Sea Research
  7. Message-ID: <4f7913$brg@sepia.nioz.nl>
  8. Reply-To: rikko@nioz.nl
  9. NNTP-Posting-Host: sepia.nioz.nl
  10.  
  11. Hi,
  12.  
  13. I have learnt C++ at my school past year, but now I have to build a programm in just
  14. plain Standard C. the problem with this is, that several C types where new to me.
  15. as example typedef.... in C++ I used a class to do this...
  16.  
  17. /*
  18.  * Data structure used for storing the loaded file.
  19.  */
  20.  
  21. typedef struct  Line {
  22.  
  23.         struct  Line  *next;    /* Pointer to next item        */
  24.         char    **dpart[3];
  25.         char    **DateLine[3];  /* contains the date in form yy-mm-dd */
  26.         char    *RestLine;      /* list of the Values of the line */
  27. } Line;
  28.  
  29. In my programm I have to load a ascii file, my approch was to build a linked list
  30. of struct line (which I defiened with typedef Line), a struct line will contain data
  31. and a pointer to the next line. this part went fine but the problems starts with
  32. clearing a file from memory, my approch to that was as follows. The errors created
  33. by this function occor in two different ways, 1) they damage a line which was loaded
  34. after the clearing 2) causes the programm to crash with a segmentation fault.
  35. I know I have made a mistake in my memeroy administration but, I can't find the problem
  36.  
  37. void ClearLines()
  38. {
  39. Line*   line;
  40. Line*   line_next;
  41.  
  42.  
  43.         line=FirstLine;
  44.  
  45.         /*
  46.          * when no file is loaded the firstline will be NULL
  47.          */
  48.  
  49.         while(line!=(Line*)NULL && line->next!=(Line*)NULL)
  50.         {
  51.                 /*
  52.                  * ask for the next line while the currentline is still around
  53.                  */
  54.  
  55.                 line_next=line->next;
  56.  
  57.                 /*
  58.                  * delete the date in both sequences
  59.                  */
  60.  
  61.                 free(line->dpart[0]);
  62.                 free(line->dpart[1]);
  63.                 free(line->dpart[2]);
  64.                 free(line->DateLine[0]);
  65.                 free(line->DateLine[1]);
  66.                 free(line->DateLine[2]);
  67.  
  68.                 /*
  69.                  * delete the rest of the line
  70.                  */
  71.  
  72.                 free(line->RestLine);
  73.  
  74.                 /*
  75.                  * delete structures of the line
  76.                  */
  77.  
  78.                 free(*line->dpart);
  79.                 free(*line->DateLine);
  80.  
  81.                 /*
  82.                  * delete the line itself and let 'line' point to the next line
  83.                  */
  84.  
  85.                 free(&line);  /* I suspect this line to trigger the error */
  86.                 line=line_next;
  87.         }
  88. }
  89.  
  90.  
  91. I hope some can find what I did wrong, I'm sure it is something simple but I can't 
  92. find it....
  93.  
  94. Thanx for reading my question
  95.  
  96. please email me if you have a solution... (rikko@nioz.nl)
  97.  
  98.     Greetings
  99.         Rikko
  100.  
  101.  
  102.